package cpm.cloud.controller;

import com.test.entities.HandDepartments;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author dell
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    //private static final String REST_URL_PREFIX = "http://localhost:8001";
    //加入ribbon后会获取该服务名称

    private static final String REST_URL_PREFIX = "http://MICROSERVICE";

    @Autowired
    private RestTemplate restTemplate;



    @GetMapping("/dept/get/{id}")
    public HandDepartments findById(@PathVariable("id")Long id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,HandDepartments.class);
    }

    @SuppressWarnings("unchecked")
    @GetMapping("/dept/list")
    public List<HandDepartments> findAll(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",List.class);
    }

    @PostMapping("/dept/add")
    public HandDepartments add(HandDepartments departments){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/add",HandDepartments.class);
    }

    @DeleteMapping("/dept/del/{id}")
    public void delete(@PathVariable Long id) {
        restTemplate.delete(REST_URL_PREFIX+"/dept/del/"+id,HandDepartments.class);
    }

}
